package com.liuym.classes.dubbo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.liuym.classes.repository.mapper.ClassesMapper;
import com.liuym.classes.repository.model.bo.ClassBO;
import com.liuym.classes.repository.model.entity.Classes;
import com.liuym.classes.service.ClassService;
import com.liuym.common.dubbo.classes.api.ClassesDubboService;
import com.liuym.common.dubbo.classes.dto.ClassDTO;
import com.liuym.common.exception.ClassException;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LiuYiMing
 * @date 2018/9/18 11:09
 * @describe：
 */
@Service(version = "${class.dubbo.version}")
public class ClassesDubboServiceImpl implements ClassesDubboService {

    @Autowired
    private ClassService classService;

    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public List<ClassDTO> getAllClass() throws ClassException {
        List<ClassBO> classBOList = classService.getAllClass();
        if (classBOList != null && classBOList.size() > 0) {
            List<ClassDTO> classDTOList =
                    classBOList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassDTO.class))
                            .collect(Collectors.toList());
            return classDTOList;
        }
        return null;
    }

    @Override
    public ClassDTO getClassByNumber(long classNumber) throws ClassException {
        ClassBO classBO = classService.getClassByNumber(classNumber);
        if (classBO != null) {
            ClassDTO classDTO = BeanMapper.map(classBO, ClassDTO.class);
            return classDTO;
        }
        return null;
    }

    @Override
    public List<ClassDTO> getClassByYear(int year) throws ClassException {
        List<Classes> classesList = classesMapper.getClassByYear(year);
        if (classesList != null && classesList.size() > 0) {
            List<ClassDTO> classDTOList =
                    classesList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassDTO.class))
                            .collect(Collectors.toList());
            return classDTOList;
        }
        return null;
    }

    @Override
    public List<Integer> getAllYear() throws ClassException {
        return classService.getAllYear();
    }

    @Override
    public int deleteClass(long classNumber) throws ClassException {
        return classService.deleteClass(classNumber);
    }

    @Override
    public int updateClass(ClassDTO classDTO) throws ClassException {
        if (classDTO != null) {
            ClassBO classBO = BeanMapper.map(classDTO, ClassBO.class);
            return classService.updateClass(classBO);
        }
        return 0;
    }

    @Override
    public int addClass(ClassDTO classDTO) throws ClassException {
        if (classDTO != null) {
            ClassBO classBO = BeanMapper.map(classDTO, ClassBO.class);
            return classService.addClass(classBO);
        }
        return 0;
    }

    @Override
    public Map<String, Object> addClassList(List<ClassDTO> classDTOList) throws ClassException {
        if (classDTOList != null && classDTOList.size() > 0) {
            List<ClassBO> classBOList =
                    classDTOList
                            .stream()
                            .map(s -> BeanMapper.map(s, ClassBO.class))
                            .collect(Collectors.toList());
            return classService.addClassList(classBOList);
        }
        return null;
    }

    @Override
    public void updateClassScoreTo80() throws ClassException {
        classService.updateClassScoreTo80();
    }
}
